package com.zhoug.common.filters;

import android.text.Editable;
import android.text.TextUtils;
import android.widget.EditText;

import com.zhoug.common.utils.LogUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 电话号码格式化:3 4 4格式(即：xxx xxxx xxxx)
 *
 * @Author: zhoug
 * @Date: 2023/8/10
 * @Description:
 */
public class PhoneFormat {
    private static boolean DEBUG = true;
    private static final String TAG = ">>>PhoneFormat";
    private static final char separator = ' ';

    /**
     * 电话3 4 4格式(即：xxx xxxx xxxx)
     * 电话长度11位数字
     *
     * @param view 输入框
     * @param text 文本
     * @return 是否改变了数据 如果要在afterTextChanged中获取实时数据的数据需要在返回值为false时获取
     * eg:
     * public void afterTextChanged(Editable s) {
     * boolean change = PhoneFormat.onTextChanged344(mEtMobileNumber, s);
     * if(!change){
     * mTvMobileNumber.setText(PhoneFormat.getPhoneNumber(s));
     * }
     * }
     */
    public static boolean onTextChanged344(EditText view, String text) {
        if (view == null || text == null || text.length() == 0) return false;
        String phoneNumber334 = getPhoneNumber334(text);
        if (phoneNumber334 != null && !phoneNumber334.equals(text)) {
            int selectionEnd = view.getSelectionEnd();
            view.setText(phoneNumber334);
            if (selectionEnd < 0) {
                view.setSelection(0);
            } else if (selectionEnd >= text.length()) {
                view.setSelection(phoneNumber334.length());
            } else {
                view.setSelection(selectionEnd);
            }
            return true;
        }
        return false;
    }


    /**
     * 电话3 4 4格式(即：xxx xxxx xxxx)
     * 电话长度11位数字
     *
     * @param editable 输入框
     * @param editable editable
     * @return 是否改变了数据
     */
    public static boolean onTextChanged344(EditText view, Editable editable) {
        return onTextChanged344(view, editable != null ? editable.toString() : null);
    }

    /**
     * 电话3 4 4格式(即：xxx xxxx xxxx)
     * 电话长度11位数字
     *
     * @param editable 输入框
     * @param editable editable
     * @return 是否改变了数据
     */
    public static boolean afterTextChangedIf334(EditText view, Editable editable) {
        return afterTextChangedIf334(view, editable != null ? editable.toString() : null);
    }

    /**
     * 电话3 4 4格式(即：xxx xxxx xxxx)
     * 电话长度11位数字
     *
     * @param view 输入框
     * @param text 文本
     * @return 是否改变了数据 如果要在afterTextChanged中获取实时数据的数据需要在返回值为false时获取
     * eg:
     * public void afterTextChanged(Editable s) {
     * boolean change = PhoneFormat.onTextChanged344(mEtMobileNumber, s);
     * if(!change){
     * mTvMobileNumber.setText(PhoneFormat.getPhoneNumber(s));
     * }
     * }
     */
    public static boolean afterTextChangedIf334(EditText view, String text) {
        if (view == null || text == null || text.length() == 0) return false;
        String formatText = formatIf334(text);
        if (formatText != null && !formatText.equals(text)) {
            int selectionEnd = view.getSelectionEnd();
            view.setText(formatText);
            if (selectionEnd < 0) {
                view.setSelection(0);
            } else if (selectionEnd >= text.length()) {
                view.setSelection(formatText.length());
            } else {
                view.setSelection(selectionEnd);
            }
            return true;
        }
        return false;
    }




    /**
     * 获得已输入的电话号，不包括空格
     *
     * @param text
     * @return
     */
    public static String getPhoneNumber(String text) {
        if (text == null) return null;
        text = text.trim();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) != separator) {
                sb.append(text.charAt(i));
            }
        }
        String result = sb.toString();
        return TextUtils.isEmpty(result) ? null : result;
    }

    /**
     * 获得已输入的电话号，不包括空格
     *
     * @param editable
     * @return 电话号
     */
    public static String getPhoneNumber(Editable editable) {
        return getPhoneNumber(editable != null ? editable.toString() : null);
    }

    /**
     * 把电话号码转化成334格式显示
     * eg:13512345678->135 1234 5678
     *
     * @param text
     * @return
     */
    public static String getPhoneNumber334(String text) {
        if (text == null || text.length() == 0) return null;
        int indexSpace1 = 3;
        int indexSpace2 = 8;
        StringBuilder sb = new StringBuilder();
        //1.取出所有字符，去掉' '和非法字符
        for (int i = 0; i < text.length(); i++) {
            //如果数字数大于11位，去掉后面的数字
            if (sb.length() >= 11) {
                break;
            }
            //是否合法字符(0~9)
            Pattern pattern = Pattern.compile("^[0-9]*$");
            Matcher matcher = pattern.matcher(String.valueOf(text.charAt(i)));
            if (text.charAt(i) != separator && matcher.matches()) {
                sb.append(text.charAt(i));
            }
        }

        //2.根据长度追加' '
        if (sb.length() > indexSpace1) {
            sb.insert(indexSpace1, separator);
        }
        if (sb.length() > indexSpace2) {
            sb.insert(indexSpace2, separator);
        }
        //3.设置文本
        String s = sb.toString();
        if (DEBUG) {
            LogUtils.d(TAG, "getPhoneNumber334:s=" + s);
        }
        return s;
    }

    /**
     * 把电话号码转化成334格式显示,非电话号码不转换
     * eg:13512345678->135 1234 5678
     *
     * @param text
     * @return
     */
    public static String formatIf334(String text) {
        if (text == null || text.length() == 0) return null;
        int indexSpace1 = 3;
        int indexSpace2 = 8;
        StringBuilder sb = new StringBuilder();
        //1.取出所有字符，去掉' '
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) != separator) {
                sb.append(text.charAt(i));
            }
        }
        //输入的是数字时
        if (Pattern.compile("\\d{1,11}").matcher(sb.toString()).matches()) {
            //2.根据长度追加' '
            if (sb.length() > indexSpace1) {
                sb.insert(indexSpace1, separator);
            }
            if (sb.length() > indexSpace2) {
                sb.insert(indexSpace2, separator);
            }
            //3.设置文本
            String s = sb.toString();
            if (DEBUG) {
                LogUtils.d(TAG, "formatLoginName:s=" + s);
            }
            return s;
        }
        return sb.toString();
    }

}
